#!/bin/bash
cd `dirname $0`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf
LOGS_DIR=$DEPLOY_DIR/logs
BIN_DIR=$DEPLOY_DIR/bin
RESTART_LOG_FILE=$LOGS_DIR/subfg-api-restart.log

APP_MAINCLASS=com.jack.payment.PaymentApplication

SERVER_PID=`ps -ef | grep -v grep | grep "$CONF_DIR" |awk '{print $2}'`
MONITOR_PID=`ps -ef | grep -v grep | grep "$BIN_DIR/monitor.sh" | awk '{print $2}'`

if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi
STDOUT_FILE=$LOGS_DIR/stdout.log
CLOG_FILE=$LOGS_DIR/gc.log

LIB_DIR=$DEPLOY_DIR/lib
LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'| xargs | sed "s/ /:/g"`

JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dspring.amqp.deserialization.trust.all=true"
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=172.17.0.1:8000,server=y,suspend=n"
JAVA_JMX_OPTS=""
if [ "$2" = "jmx" ]; then
    JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi

JAVA_MEM_OPTS=""
#JAVA_MEM_OPTS="-server -Xms5120M -Xmx5120M -Xmn1024M -Xnoclassgc -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=80 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintClassHistogram -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:$CLOG_FILE"

function start_server() {
	echo -e "starting the subfg-api server ..."

	if [ "$2" = "debug" ]; then
	        java -Dapp.home=$DEPLOY_DIR $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $CONF_DIR:$LIB_JARS $APP_MAINCLASS
		return 0
	else
	        nohup java -Dapp.home=$DEPLOY_DIR $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $CONF_DIR:$LIB_JARS $APP_MAINCLASS  >/dev/null  2>&1 &

	fi

	sleep 1
	echo "subfg-api server started"
	SERVER_PID=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
	echo "SERVER_PID: $SERVER_PID"

	if [ ! "$2" = "debug" ]; then
		if [ ! -f $RESTART_LOG_FILE ];then
			touch $RESTART_LOG_FILE
			echo "create restart log file $RESTART_LOG_FILE"
		fi

		MONITOR_PID=`ps -ef | grep -v grep | grep "$BIN_DIR/monitor.sh" | awk '{print $2}'`
		if [ ! -n "$MONITOR_PID" ];then
			echo "monitor not started start it"
			nohup bash $BIN_DIR/monitor.sh >/dev/null  2>&1 &
			MONITOR_PID=`ps -ef | grep -v grep | grep "$BIN_DIR/monitor.sh" | awk '{print $2}'`
			echo "MONITOR_PID: $MONITOR_PID"
		else
			echo "monitor already started MONITOR_PID: $MONITOR_PID"
		fi
	fi
}

function start_server_debug() {
	echo -e "starting the subfg-api server ..."
	java -Dapp.home=$DEPLOY_DIR $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS  -classpath $CONF_DIR:$LIB_JARS $APP_MAINCLASS
}

function stop_server() {
	MONITOR_PID=`ps -ef | grep -v grep | grep "$BIN_DIR/monitor.sh" | awk '{print $2}'`
	echo "MONITOR_PID: $MONITOR_PID"
	if [ -z "$MONITOR_PID" ]; then
	    echo "monitor does not started!"
	else
		echo -e "stopping monitor ...\c"
		kill $MONITOR_PID > $STDOUT_FILE 2>&1

		MONITOR_COUNT=0
		while [ $MONITOR_COUNT -lt 1 ]; do
		    echo -e ".\c"
		    sleep 1
		    MONITOR_COUNT=1
		    PID_EXIST=`ps -ef | grep -v grep | grep "$BIN_DIR/monitor.sh" | awk '{print $2}'`
		    if [ -n "$PID_EXIST" ]; then
			MONITOR_COUNT=0
		    fi
		done
		echo "monitor stopped"
		echo "MONITOR_PID: $MONITOR_PID"
	fi

	SERVER_PID=`ps -ef | grep -v grep | grep "$DEPLOY_DIR/conf" | awk '{print $2}'`
	echo "SERVER_PID: $SERVER_PID"
	if [ -z "$SERVER_PID" ]; then
	    echo "ERROR: the subfg-api server does not started!"
	    exit 1
	fi

	echo -e "stopping the subfg-api server ...\c"
	kill $SERVER_PID > $STDOUT_FILE 2>&1

	COUNT=0
	while [ $COUNT -lt 1 ]; do
	    echo -e ".\c"
	    sleep 1
	    COUNT=1
	    PID_EXIST=`ps -ef | grep -v grep | grep "$DEPLOY_DIR/conf" | awk '{print $2}'`
	    if [ -n "$PID_EXIST" ]; then
		COUNT=0
	    fi
	done

	echo "stopped"
	echo "SERVER_PID: $SERVER_PID"
}

case "$1" in
    'start')
        start_server
        ;;
    'start-debug')
        start_server_debug
        ;;
    'stop')
        stop_server
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac
